package com;

import java.io.*;
import java.net.*;

public class Server extends ServerSocket {
	private static final int SERVER_PORT = 10000;

	public Server() throws IOException {
		super(SERVER_PORT);

		try {
			while (true) {
				Socket socket = accept();
				new CreateServerThread(socket);
				//socket.close();
			}
		} catch (IOException e) {
		} finally {
			//close();
		}
	}

	// --- CreateServerThread
	class CreateServerThread extends Thread {
		private Socket client;
		private BufferedReader in;
		private PrintWriter out;

		public CreateServerThread(Socket s) throws IOException {
			client = s;

			in = new BufferedReader(new InputStreamReader(
					client.getInputStream(), "GB2312"));

			out = new PrintWriter(client.getOutputStream(), true);
			out.println("--- Welcome ---");
			start();
		}

		public void run() {
			System.out.println("thread start");
			try {
				char[] data = new char[1024];
				byte[] b = new byte[1024];
				client.getInputStream().available();
				in = new BufferedReader(new InputStreamReader(
						client.getInputStream(), "GB2312"));
				in.read(data);
				
				System.out.println(new String(data));
				out.println("1111");
				System.out.println("--- See you, bye! ---");
				in.close();
				out.close();
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		private String createMessage(String line) {
			// xxxxxxxxx;
			return line;
		}
	}

	public static void main(String[] args) throws IOException {
		new Server();
	}
}